﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class AircraftCabins : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FH1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Request.QueryString["tail_no"] == null)
                {
                    lblError.Text = "Invalid page call!";
                }
                else
                {
                    lblTailNo.Text = Request.QueryString["tail_no"].ToString();
                }
            }
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FH2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you want to delete.";
                return;
            }

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_aircraft_delete_cabin";
                SqlParameter para = cmd.Parameters.AddWithValue("@tail_no", lblTailNo.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@cabin", Request.Form["KEY"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed!";
                else
                    lblError.Text = "Error!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FH2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_aircraft_add_cabin";
                SqlParameter para = cmd.Parameters.AddWithValue("@tail_no", lblTailNo.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@cabin", lstCabins.SelectedValue);
                para = cmd.Parameters.AddWithValue("@capacity", txtCapacity.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Inserting failed!";
                else
                    lblError.Text = "Error!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}